from typing import List

from database.mapper.stock_day_info import StockDayInfoDto
from utils import json_util, date_util


# 翻译消息
def translation_info(info, type):
    msg_dict = {}
    if type == 1:
        code = info['code']
        msg_dict['\n名称:'] = info['name']
        msg_dict[',编码:'] = code
        msg_dict['\n触发时间:'] = info['trigger_time']
        msg_dict['\n当前macd形态:'] = '"' + info['macd'] + '"'
        if 'deal_price' in info.keys():
            msg_dict['\n触发价格:'] = str(info['deal_price'])
        msg_dict['\n当前价格:'] = str(info['rt_price'])
        msg_dict['\n5日均线价:'] = str(info['ave5'])
        msg_dict['\n10日均线价:'] = str(info['ave10'])
        msg_dict['\n超大单净流入:'] = str(int(info['sb_io'] / 10000)) + '万'
        msg_dict['\n大单净流入:'] = str(int(info['b_io'] / 10000)) + '万'
        msg_dict['\n换手率:'] = str(info['hsl'])
        msg_dict['\n触发标志:'] = info['result_file_path']
        return msg_dict
    if type == 3:
        mr = json_util.to_json(info)
        try:
            # {'five_main_io': -24910.0, 'ten_main_io': 9648.0, 'thirty_main_io': -80496.0, 'sixty_main_io': -121638.0,
            # 'hundred_main_io': -242902.0, 'fund_list_len': 17, 'total_val': 278, 'cir_val': 278,
            # 'fund_hold': 12.0, 'fund_hold_total_prop': 4.4, 'fund_hold_cir_prop': 4.4, 'five_jgcyd_ave': 31.63,
            # 'ten_jgcyd_ave': 30.91, 'fifteen_jgcyd_ave': 29.19, 'twenty_jgcyd_ave': 29.17, 'all_jgcyd_ave': 28.7}
            msg_dict['\n5日主力净入:'] = str(round(mr['five_main_io'] / 10000, 2)) + '亿'
            msg_dict['\n10日主力净入:'] = str(round(mr['ten_main_io'] / 10000, 2)) + '亿'
            msg_dict['\n30日主力净入:'] = str(round(mr.get('thirty_main_io') / 10000, 2)) + '亿'
            msg_dict['\n60日主力净入:'] = str(round(mr['sixty_main_io'] / 10000, 2)) + '亿'
            msg_dict['\n100日主力净入:'] = str(round(mr['hundred_main_io'] / 10000, 2)) + '亿'
            msg_dict['\n总市值:'] = str(mr['total_val']) + '亿'
            msg_dict['\n流通市值:'] = str(mr['cir_val']) + '亿'
            msg_dict['\n基金数量:'] = str(mr['fund_list_len']) + '只'
            msg_dict['\n基金持有:'] = str(int(mr['fund_hold'])) + '亿'
            msg_dict['\n基金总占比:'] = str(mr['fund_hold_total_prop']) + '%'
            msg_dict['\n基金流通占比:'] = str(mr['fund_hold_cir_prop']) + '%'
            msg_dict['\n5日机构参与度:'] = str(mr['five_jgcyd_ave']) + '%'
            msg_dict['\n10日机构参与度:'] = str(mr['ten_jgcyd_ave']) + '%'
            msg_dict['\n15日机构参与度:'] = str(mr['fifteen_jgcyd_ave']) + '%'
            msg_dict['\n20日机构参与度:'] = str(mr['twenty_jgcyd_ave']) + '%'
            msg_dict['\n' + str(mr['flow_jgcyd_len']) + '日机构参与度:'] = str(mr['all_jgcyd_ave'])
        except Exception as e:
            print('error' + str(e))
        return msg_dict
    return msg_dict


# 处理日信息
def translation_day_info(d_list):
    msg_arr = [['名称', '编码', '日期', '主力流入', '主力流出', '主力净流入',
                '超大单净流入', '大单净流入', '中单净流入', '小单净流入', '换手率', '总市值', '流通市值']]
    for di in d_list:
        msg_arr.append([di['name'], di['code'],
                        date_util.date_to_str(di['rq'], '%Y-%m-%d'),
                        str(round(di['main_i'] / 100000000, 2)) + '亿',
                        str(round(di['main_o'] / 100000000, 2)) + '亿',
                        str(round(di['main_io'] / 100000000, 2)) + '亿',
                        # str(round(di['sb_i'] / 100000000, 2)) + '亿',
                        # str(round(di['sb_o'] / 100000000, 2)) + '亿',
                        str(round(di['sb_io'] / 100000000, 2)) + '亿',
                        # str(round(di['b_i'] / 100000000, 2)) + '亿',
                        # str(round(di['b_o'] / 100000000, 2)) + '亿',
                        str(round(di['b_io'] / 100000000, 2)) + '亿',
                        # str(round(di['m_i'] / 100000000, 2)) + '亿',
                        # str(round(di['m_o'] / 100000000, 2)) + '亿',
                        str(round(di['m_io'] / 100000000, 2)) + '亿',
                        # str(round(di['s_i'] / 100000000, 2)) + '亿',
                        # str(round(di['s_o'] / 100000000, 2)) + '亿',
                        str(round(di['s_io'] / 100000000, 2)) + '亿',
                        str(round(di['turnover_rate'], 2)) + '%',
                        str(round(di['market_value'] / 100000000, 2)) + '亿',
                        str(round(di['circulation_value'] / 100000000, 2)) + '亿'])
    return msg_arr


def trans_day_info(d_list: List[StockDayInfoDto]):
    msg_arr = [['名称', '编码', '日期', '主力流入', '主力流出', '主力净流入',
                '超大单净流入', '大单净流入', '中单净流入', '小单净流入', '换手率', '总市值', '流通市值']]
    for di in d_list:
        msg_arr.append([di.name, di.code, di.rq,
                        str(round(di.main_i / 100000000, 2)) + '亿',
                        str(round(di.main_o / 100000000, 2)) + '亿',
                        str(round(di.main_io / 100000000, 2)) + '亿',
                        # str(round(di.sb_i / 100000000, 2)) + '亿',
                        # str(round(di.sb_o / 100000000, 2)) + '亿',
                        str(round(di.sb_io / 100000000, 2)) + '亿',
                        # str(round(di.b_i / 100000000, 2)) + '亿',
                        # str(round(di.b_o / 100000000, 2)) + '亿',
                        str(round(di.b_io / 100000000, 2)) + '亿',
                        # str(round(di.m_i / 100000000, 2)) + '亿',
                        # str(round(di.m_o / 100000000, 2)) + '亿',
                        str(round(di.m_io / 100000000, 2)) + '亿',
                        # str(round(di.s_i / 100000000, 2)) + '亿',
                        # str(round(di.s_o / 100000000, 2)) + '亿',
                        str(round(di.s_io / 100000000, 2)) + '亿',
                        str(round(di.hsl, 2)) + '%',
                        str(round(di.market_val / 100000000, 2)) + '亿',
                        str(round(di.cir_val / 100000000, 2)) + '亿'])
    return msg_arr
